package com.das.mechanic_base.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.das.mechanic_base.R;
import com.das.mechanic_base.utils.X3ScreenUtils;

public class X3AppPrograssView extends View {

    private Context mContext;

    private Paint mPaint;

    private double prograss = 0;
    private float stoke_width;
    private int stoke_color;

    public X3AppPrograssView(Context context) {
        this(context,null);
    }

    public X3AppPrograssView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public X3AppPrograssView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.AppPrograssView, 0, 0);
        stoke_width = typedArray.getDimension(R.styleable.AppPrograssView_stoke_width, X3ScreenUtils.dipToPx(5,mContext));
        stoke_color = typedArray.getColor(R.styleable.AppPrograssView_stoke_color,Color.parseColor("#0077ff"));

        initView();
    }

    private void initView() {
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(stoke_width);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int radius_width = (int) (getWidth() / 2 - stoke_width);
        int center_x = getWidth() / 2;
        int center_y = getHeight() / 2;

        // 绘制底色
        mPaint.setColor(Color.parseColor("#20ffffff"));
        canvas.drawCircle(center_x,center_y,radius_width,mPaint);

        mPaint.setColor(stoke_color);
        // 绘制进度
        RectF rect = new RectF(center_x - radius_width + 1,center_y - radius_width + 1,
                center_x + radius_width - 1,center_y + radius_width -1);
        canvas.drawArc(rect,270,  (float)(prograss * 0.01) * 360,false,mPaint);
    }

    /**
     * 设置当前进度
     * @param prograss
     */
    public void setCurrentPrograss(double prograss) {
        this.prograss = prograss;
        invalidate();
    }
}
